package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * 套餐管理
 */
@Api(value = "/admin/setmeal", tags = {"套餐管理"})
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐功能
     * @param setmealDTO 添加套餐请求数据
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SetmealDTO", name = "setmealDTO", value = "添加套餐请求数据", required = true)
    })
    @ApiOperation(value = "添加套餐功能", notes = "添加套餐功能", httpMethod = "POST")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        return setmealService.insertSetmeal(setmealDTO);
    }

    /**
     * 分页显示所有套餐信息
     * @param setmealPageQueryDTO 分页请求信息
     * @return
     */
    @ApiOperation(value = "分页显示所有套餐信息", notes = "分页显示所有套餐信息", httpMethod = "GET")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        return setmealService.pageQuerySetmeal(setmealPageQueryDTO);
    }

    /**
     * 套餐起售/停售功能
     * @param status 状态信息
     * @param id 套餐id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "status", value = "状态信息", required = true)
    })
    @ApiOperation(value = "套餐起售/停售功能", notes = "套餐起售/停售功能", httpMethod = "POST")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result updateSetmealStatus(@PathVariable("status") Integer status,Long id) {
        return setmealService.updateSetmealStatus(status,id);
    }

    /**
     * 套餐删除功能
     * @param ids 多个套餐id
     * @return
     */
    @ApiOperation(value = "套餐删除功能", notes = "套餐删除功能", httpMethod = "DELETE")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result delete(String ids) {
        return setmealService.delete(ids);
    }

    /**
     * 回显套餐信息
     * @param id 套餐id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "套餐id", required = true)
    })
    @ApiOperation(value = "回显套餐信息", notes = "回显套餐信息", httpMethod = "GET")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmeal(@PathVariable("id") Long id) {
        return setmealService.getSetmeal(id);
    }

    /**
     * 更新套餐功能
     * @param setmealDTO 更新套餐请求参数
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SetmealDTO", name = "setmealDTO", value = "更新套餐请求参数", required = true)
    })
    @ApiOperation(value = "更新套餐功能", notes = "更新套餐功能", httpMethod = "PUT")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        return setmealService.update(setmealDTO);
    }
}
